package com.qidian.mes.defect.controller;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageInfo;
import com.qidian.mes.defect.service.ShipmentService;
import com.qidianit.common.pojo.MESResult;
import com.qidianit.mes.pojo.Incoming;
import com.qidianit.mes.pojo.Shipment;
import com.qidianit.mes.pojo.TestProcess;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @Author 泽华
 * @Description
 * @Time 2024-10-24 0:39
 */
@RestController
@CrossOrigin
@RequestMapping("/shipment")

public class ShipmentController {
    @Autowired
    private ShipmentService shipmentService;
    @GetMapping("/list")
    public MESResult list(Shipment shipment, @RequestParam(defaultValue = "1")int page, @RequestParam(defaultValue = "10") int limit){
        return shipmentService.list(shipment, page, limit);
    }

    @PostMapping("/save")
    public MESResult save(@RequestBody Shipment shipment){
        return shipmentService.save(shipment);
    }


    @PutMapping("/update")
    public MESResult update(@RequestBody Shipment shipment){
        return shipmentService.update(shipment);
    }

    @GetMapping("/get")
    public MESResult get(Long shipmentId){
        return shipmentService.selectById(shipmentId);
    }


    @GetMapping("/export")
    public MESResult export(Shipment shipment, int page, int limit) {
        // 查询数据
        MESResult result = shipmentService.list(shipment, 1, 10);
        PageInfo<Shipment> pageInfo = (PageInfo<Shipment>) result.getData();
        List<Shipment> shipments = pageInfo.getList();
        //  通过EasyExcel写数据
        int num = 200;
        try{
            EasyExcel.write("E:\\MES\\Excel\\shipment-"+ page + limit + ".xlsx",
                            Shipment.class)
                    .sheet("出货检验数据")
                    .doWrite(shipments);
        }catch (Exception e){
            num = 300;
        }
        return MESResult.success(num,"OK");
    }



}
